{% extends 'account/base.html' %}
{% block css %}
    <style>
        .error-msg {
            color: red;
            position: absolute;
            font-size: 12px;
        }
    </style>
{% endblock %}


{% block content %}
<div class="bg-white">
    <div class="container">
        <div class="row justify-content-center align-items-center d-flex vh-100">
            <div class="col-md-4 mx-auto">
                <div class="osahan-login py-4">
                    <div class="text-center mb-4">
                        <a href="index.html"><img src="img/fav.png" alt=""></a>
                        <h5 class="font-weight-bold mt-3">Join Universe 🌏</h5>
                        <p class="text-muted">Make the most of your life</p>
                    </div>

                    <form id="regForm" method="POST" novalidate>
                        {% csrf_token %}
                        {% for field in form %}

                            {% if field.name == 'code' %}
                                <div class="form-row">
                                    <div class="col">
                                        <div class="form-group">
                                            <label class="mb-1" for="{{ field.id_for_label }}">{{ field.label }}</label>
                                            <div class="position-relative icon-form-control">
                                                <i class="feather-crosshair position-absolute"></i>
                                                {{ field }}
                                                <span class="error-msg"></span>
                                            </div>
                                        </div>
                                    </div>


                                    <div class="col">
                                        <div class="form-group">
                                            <label class="mb-1">👻<i class="feather-send"></i></label>
                                            <div class="position-relative icon-form-control">

                                                <input id="btnSms" type="button" class="btn btn-primary btn-block text-uppercase" value="点击获取验证码">
                                            </div>
                                        </div>
                                    </div>
                                </div>



                        <!--    英文格式用户名
                        <div class="form-row">
                            <div class="col">
                                <div class="form-group">
                                    <label class="mb-1">First name</label>
                                    <div class="position-relative icon-form-control">
                                        <i class="feather-user position-absolute"></i>
                                        <input type="text" class="form-control">
                                    </div>
                                </div>
                            </div>
                            <div class="col">
                                <div class="form-group">
                                    <label class="mb-1">Last name</label>
                                    <div class="position-relative icon-form-control">
                                        <i class="feather-user position-absolute"></i>
                                        <input type="text" class="form-control">
                                    </div>
                                </div>
                            </div>
                        </div>
                        -->

                            {% else %}
                                <div class="form-group">
                                    <label class="mb-1" for="{{ field.id_for_label }}">{{ field.label }}</label>
                                    <div class="position-relative icon-form-control">
                                        <!-- 先不要添加图标
                                        <i class="feather-at-sign position-absolute"></i> -->
                                        {% if field.name == 'username' %}
                                            <i class="feather-user position-absolute"></i>
                                        {% elif field.name == 'password' or field.name == 'confirm_password' %}
                                            <i class="feather-lock position-absolute"></i>
                                        {% elif field.name == 'mobile_phone' %}
                                            <i class="feather-smartphone position-absolute"></i>
                                        {% endif %}

                                        {{ field }}
                                        <span class="error-msg"></span>
                                    </div>
                                </div>


                            {% endif %}
                        {% endfor %}

                        <div class="form-group">
                            <label class="mb-1">You agree to the universe <a href="#">User Agreement</a>, <a href="#">Privacy
                                Policy</a>, and <a href="#">Cookie Policy</a>.</label>
                        </div>


                        <button id="btnSubmit" class="btn btn-primary btn-block text-uppercase" type="button"> Agree & Join</button>

                    </form>


                        <!-- 其他方式登录 -->
                        <div class="text-center mt-3 border-bottom pb-3">
                            <p class="small text-muted">Or login with</p>
                            <div class="row">
                                <div class="col-4">
                                    <button type="button" class="btn btn-sm btn-outline-instagram btn-block"><i
                                            class="feather-instagram"></i> Instagram
                                    </button>
                                </div>
                                <div class="col-4">
                                    <button type="button" class="btn btn-sm btn-outline-linkedin btn-block"><i
                                            class="feather-linkedin"></i> Linkedin
                                    </button>
                                </div>
                                <div class="col-4">
                                    <button type="button" class="btn btn-sm btn-outline-facebook btn-block"><i
                                            class="feather-facebook"></i> Facebook
                                    </button>
                                </div>
                            </div>
                        </div>

                        <!-- 忘记密码 -->
                        <div class="py-3 d-flex align-item-center">
                            <a href="forgot-password.html">Forgot password?</a>
                            <span class="ml-auto"> Already on Osahanin? <a class="font-weight-bold" href="{% url 'login' %}">Sign in</a></span>
                        </div>


                </div>
            </div>
        </div>
    </div>
</div>

{% endblock %}




{% block js %}
    <script>
        // 页面框架加载完成之后自动执行函数
        $(function () {
            bindClickBtnSms();
            bindClickSubmit();
        });

        /*
        点击提交（注册）
        */
        function bindClickSubmit() {
            $('#btnSubmit').click(function () {
                $('.error-msg').empty();
                // 收集表单中的数据（找到每一个字段）$('#regForm').serialize()
                // 数据ajax发送到后台
                $.ajax({
                    url: "{% url 'register' %}",
                    type: "POST",
                    data: $('#regForm').serialize(), // 所有字段数据 + csrf token
                    dataType: "JSON",
                    success: function (res) {
                        if(res.status){
                            location.href = res.data;
                            alert("恭喜您注册成功");
                        }else{
                            $.each(res.error, function (key, value) {
                                $("#id_" + key).next().text(value[0]);
                            })
                        }
                    }
                })
            })
        }

        /*
        点击获取验证码的按钮绑定事件
         */
        function bindClickBtnSms() {
            $('#btnSms').click(function () {

                $('.error-msg').empty();

                // 获取用户输入的手机号
                // 找到输入框的ID，根据ID获取值，如何找到手机号的那个ID？
                var mobilePhone = $('#id_mobile_phone').val();

                // 发送ajax请求，把手机号发送过去
                $.ajax({
                    url: "{% url 'send_sms' %}", // 等价于 /send/sms/
                    type: "GET",
                    data: {mobile_phone: mobilePhone, tpl: "register"},
                    dataType: "JSON", // 将服务端返回的数据反序列化为字典
                    success: function (res) {
                        // ajax请求发送成功之后，自动执行的函数； res就是后端返回的值
                        if (res.status) {
                            sendSmsRemind();
                        } else {
                            // 错误信息
                            // console.log(res); // {status:False, error:{ mobile_phone: ["错误信息"，],code: ["错误信息"，] }  }
                            $.each(res.error, function (key, value) {
                                $("#id_" + key).next().text(value[0]);
                            })
                        }
                    }
                })

            })
        }

        /*
        倒计时
         */
        function sendSmsRemind() {
            var $smsBtn = $('#btnSms');
            $smsBtn.prop('disabled', true); // 禁用
            var time = 5;
            var remind = setInterval(function () {
                $smsBtn.val(time + '秒重新发送');
                time = time - 1;
                if (time < 1) {
                    clearInterval(remind);
                    $smsBtn.val('点击获取验证码').prop('disabled', false);
                }
            }, 1000)

        }



    </script>
{% endblock %}
